// pages/roomsList/roomsList.js
var common = require('../../utils/common.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // dataList: [{}, {}],
    status: '',//请求接口的出租状态0未租，1已租。空位全部
    house_id: '',//房屋id
    tabnav: {},  //顶部选项卡数据7
    showtab: 0,  //顶部选项卡索引
    dataList: [],//全部房间
    page: 1,//请求接口的页码
    forRentDataList: [],//已经出租数据
    forRentPage: 1,//页码
    waitingForRentDataList: [],//等待出租页面
    waitingPage: 1,//页码
  },

  setTab: function (e) { //设置选项卡选中索引
    const edata = e.currentTarget.dataset;
    this.setData({
      showtab: edata.tabindex,
    })
    if (edata.tabindex == 1) {
      //已租
      this.setData({
        status: '1'
      })
      if (this.data.forRentDataList.length < 1) {
        console.log("已租")
        this.getDataList();
      }
    }
    else if (edata.tabindex == 2) {
      //未租
      this.setData({
        status: '0'
      })
      if (this.data.waitingForRentDataList.length < 1) {
        this.getDataList();
      }
    }
    else {
      //全部
      this.setData({
        status: ''
      })
      if (this.data.dataList.length < 1) {
        this.getDataList();
      }
    }


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      page: 1,
      house_id: options.house_id,
      tabnav: {
        tabnum: 3,
        tabitem: [
          {
            "id": 0,
            "type": "",
            "text": "全部",
            "count":''
          },
          {
            "id": 1,
            "type": "A",
            "text": "已出租",
            "count": ''
          },
          {
            "id": 2,
            "type": "A",
            "text": "未出租",
            "count": ''
          }
        ]
      },
      uploadimgs: []
    })
    // wx.showLoading({
    //   title: '加载中',
    // })
    // this.getDataHttps(true);
  },
  //添加房间
  addRooms:function(){
    wx.navigateTo({
      url: '../addRooms/addRooms?house_id=' + this.data.house_id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getDataCount();
    this.getDataList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var xgRooms = wx.getStorageSync('modifyRoom')
    wx.removeStorageSync('modifyRoom')
    if (xgRooms){
      this.allRefresh();
      this.getDataList();
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getDataCount();
    //下拉刷新
    if (this.data.showtab == 0) {//全部
      this.setData({
        dataList: [],
        page:1,
      })
    } else if (this.data.showtab == 1) {//已租
      this.setData({
        forRentDataList: [],
        forRentPage:1,
      })

    } else if (this.data.showtab == 2) {//未租
      this.setData({
        waitingForRentDataList: [],
        waitingPage:1,
      })
    }
    this.getDataList();
  },
  allRefresh:function(){
       
      this.setData({
        dataList: [],
        page: 1,
        forRentDataList: [],
        forRentPage: 1,
        waitingForRentDataList: [],
        waitingPage: 1,
      })
    this.getDataList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //上拉加载更多
    // this.getDataList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  roomitemclick: function (e) {
    // var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../waitingRentRoom/waitingRentRoom?room_id=' + id
      })
  },
  getDataCount : function(){
    var that = this;
    //获取统计数据
    common._get('/wx/room/total', { houseId: that.data.house_id },
      //成功
      function (res) {
        console.log(res.data)
        if (res.data.success) {
          that.data.tabnav.tabitem[0].count = res.data.totalCount
          that.data.tabnav.tabitem[2].count = res.data.unRentedCount
          that.data.tabnav.tabitem[1].count = res.data.rentedCount
          that.setData({
            tabnav: that.data.tabnav
          })
        }
      });
  },
  /**
   * https请求获取数据
   * pageNumber页码
   * isRefresh 是否是刷新，还是上拉加载更多。true刷新；false加载更多
   */
  getDataList: function () {
    var that = this;
    var pagec = "";
    if (that.data.showtab == 0) {//全部
      pagec = that.data.page;
    } else if (that.data.showtab == 1) {//已租
      pagec = that.data.forRentPage;
    } else if (that.data.showtab == 2) {//未租
      pagec = that.data.waitingPage;
    }
    var getData = {
      houseId: that.data.house_id,
      status: that.data.status,
    }
    console.log(getData)
    //获获取房间列表
    common._get('/wx/room/roomlist', getData,
      //成功
      function (res) {
        console.log('res.data');
        console.log(res.data);
        if (res.data.success) {
          var temp = [];
          if (that.data.showtab == 0) {//全部
            // temp = that.data.dataList.concat(res.data.data);
            that.setData({
              dataList: res.data.data,
            })
            // that.data.page++;
          } else if (that.data.showtab == 1) {//已租
            // temp = that.data.forRentDataList.concat(res.data.data);
            that.setData({
              forRentDataList: res.data.data,
            })
            // that.data.forRentPage++;

          } else if (that.data.showtab == 2) {//未租
            // temp = that.data.waitingForRentDataList.concat(res.data.data);
            that.setData({
              waitingForRentDataList: res.data.data,
            })
            // that.data.waitingPage++;
          }
        }
      });

   

  }

})